package tv.scolopender.json;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;

import tv.scolopender.model.User;

public class UserJSON {
	static String location = "http://hdmk-portal.net/danny/scolopenderTV/";
	static String coding = "UTF-8";

	public static void insert(User user) {
		try {

			ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
			nameValuePairs.add(new BasicNameValuePair("uid", user.getId()));

			// http post
			HttpClient httpclient = new DefaultHttpClient();
			HttpPost httppost = new HttpPost(location + "insertUser.php");
			httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
			HttpResponse response = httpclient.execute(httppost);
			HttpEntity entity = response.getEntity();
			InputStream is = entity.getContent();
			// convert response to string
			BufferedReader reader = new BufferedReader(new InputStreamReader(
					is, coding), 8);
			StringBuilder sb = new StringBuilder();
			String line = null;

			while ((line = reader.readLine()) != null) {
				sb.append(line + "\n");
			}

			is.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
